# based on xslauth.py by Pat LaCosse, for editing XSL in NotePad++

def stylesheet(s, params=None, output=None, namespaces={}):
    return """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"%s>%s %s %s</xsl:stylesheet>""" % (
        " ".join(["xmlns:%s='%s'" % (k, namespaces[k]) for k in namespaces.keys()]), params or '', output or output_tag(method='xml'), s)

def param_tag(name, select=None):
    return """<xsl:param name="%s" %s/>""" % (name, select and ('select="'+select+'"') or '')

def output_tag(method='xml', **params):
    return """<xsl:output method="%s" %s/>""" % (method, ''.join(["""%s="%s" """ % (k, params[k]) for k in params.keys()]))

def copy_all():
    return """<xsl:template match="@*|node()" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:copy xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:apply-templates select="@*|node()" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/></xsl:copy></xsl:template>"""

def copy(val):
    return """<xsl:copy xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:copy>""" % val

def copy_select(select, val):
    return """<xsl:copy select="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:copy>""" % (select, val)

def copy_of(select):
    return """<xsl:copy-of select="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>""" % select

def choose(test, when, otherwise=''):
    return """<xsl:choose xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:when test="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:when><xsl:otherwise xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:otherwise></xsl:choose>""" % (test, when, otherwise)

def template_match(match, val):
    return """<xsl:template match="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:template>""" % (match, val)

def template_match_mode(match, mode, val):
    return """<xsl:template match="%s" mode="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:template>""" % (match, mode, val)
            
def template_name(name, val):
    return """<xsl:template name="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:template>""" % (name, val)

def template_match_omission(match):
    return """<xsl:template match="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>""" % match

def apply_templates():
    return """<xsl:apply-templates xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>"""

def apply_templates_mode(mode):
    return """<xsl:apply-templates mode="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>""" % mode

def apply_templates_select(select):
    return """<xsl:apply-templates select="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>""" % select

def apply_templates_select_mode(select, mode):
    return """<xsl:apply-templates select="%s" mode="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>""" % (select, mode)

def element(name, val):
    return """<xsl:element name="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:element>""" % (name, val)
    
def attribute(name, val):
    return """<xsl:attribute name="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:attribute>""" % (name, val)

def variable(name, val):
    return """<xsl:variable name="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:variable>""" % (name, val)

def variable_select(name, select):
    return """<xsl:variable name="%s" select="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>""" % (name, select)

def value_of(select):
    return """<xsl:value-of select="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>""" % select

def text(t):
    return """<xsl:text xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:text>""" % t

def for_each(select, val):
    return """<xsl:for-each select="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:for-each>""" % (select, val)

def if_test(test, val):
    return """<xsl:if test="%s" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</xsl:if>""" % (test, val)

def newline():
    return """<xsl:text xmlns:xsl="http://www.w3.org/1999/XSL/Transform">&#xa;</xsl:text>"""

def nbsp():
    return """<xsl:text xmlns:xsl="http://www.w3.org/1999/XSL/Transform">&#x00a0;</xsl:text>"""

def p(val):
    return """<p>%s</p>""" % val

def p_class(clas, val):
    return """<p class="%s">%s</p>""" % (clas, val)

def span_class(clas, val):
    return """<span class="%s">%s</span>""" % (clas, val)

def output_method(method):
    return """<xsl:output method="%s" encoding="utf-8" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>""" % method

# Still to add: xsl:output, xsl:include, xsl:copy, xsl:copy-of, xsl:param, xsl:apply-templates select|match, xsl:call-template
